import { Cloud } from 'laf-client-sdk'
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
const cloud = new Cloud({
  baseUrl: 'https://su75ks.laf.run', // <APP_ID> 在首页应用列表获取
  getAccessToken: () => userStore.token, // 这里不需要授权，先填空
})

//获取发送的消息
export async function getSendMessage(_id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'PB_Inf',
    operation: 'get',
    data: {
      where: {
        sender_id: _id,
      },
      orderBy: {
        create_time: 'desc',
      },
    },
  })
  return res
}

//获取接收的消息
export async function getReceiveMessage(_id) {
  const resNotice = await cloud.invoke('curd_op', {
    collection: 'PB_Inf',
    operation: 'get',
    data: {
      where: {
        receiver_id: _id,
      },
      orderBy: {
        create_time: 'desc',
      },
    },
  })
  const resActivity = await cloud.invoke('curd_op', {
    collection: 'PB_Inf',
    operation: 'get',
    data: {
      where: {
        sender_id: '65def6cb41b7db4b9f3fb6c9',
        utype: 1,
      },
      orderBy: {
        create_time: 'desc',
      },
    },
  })
  return { resNotice, resActivity }
}

//已发送数据分页查询
export async function getPBInfoSend(page, pageSize, _id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'PB_Inf',
    operation: 'get',
    data: {
      where: {
        sender_id: _id,
      },
      limit: pageSize,
      skip: (page - 1) * pageSize,
    },
  })
  return res
}

//已接收数据分页查询
export async function getPBInfoReceive(page, pageSize, _id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'PB_Inf',
    operation: 'get',
    data: {
      where: {
        receiver_id: _id,
      },
      limit: pageSize,
      skip: (page - 1) * pageSize,
    },
  })
  return res
}
